$(function(){
  // 定义全局变量bookType,bookInfo
  var bookType = '';
  var bookInfo = '';
  var getBookInfo = function(){
    var searchType = bookType;
    var searchBookInfo = bookInfo;
    console.log(searchType); //分别为bookName, bookAutor, BookISBN
    console.log(searchBookInfo); //用户输入的内容
    if(searchBookInfo == ''){
      alert('请先输入查询信息！')
    }else{
      $.ajax({
        type: "POST",
        url: "../php/book/searchbook.php", //PHP文件地址
        data: {searchBooInfo: searchBookInfo, searchBookType: searchType},
        // data: { AlbumName: "shanghai", Entered: "5/9/2013" },
        dataType: "json",
        success: function (response) {
          console.log(response);
          if(response == 'null'){
            alert("对不起，暂无该书籍的查询结果！")
          }else{
            $(".formContainer .form").empty();
            var $li = $("<li class='des'>"+
            "<div class='bookName'>书名</div>"+
            "<div class='bookAutor'>作者</div>"+
            "<div class='isbn'>ISBN</div>"+
            "<div class='type'>类别</div>"+
            "<div class='num'>总库存</div>"+
            "<div class='reNum'>剩余库存</div>"+
            "<div class='position'>位置</div>"+
            "<div class='operation'>库存管理</div>"+
            "</li>");
            $(".formContainer .form").append($li);
    
            for(let i = 0; i < response.length ;i++){
              var bookObj = response[i];
              var $li = $("<li class='bookResult'>"+
              "<div class='bookName'>"+bookObj.Bname+"</div>"+
              "<div class='bookAutor'>"+bookObj.Bauthor+"</div>"+
              "<div class='isbn'>"+bookObj.ISBN+"</div>"+
              "<div class='type'>"+bookObj.Bclass+"</div>"+
              "<div class='num'>"+bookObj.Bnum+"</div>"+
              "<div class='reNum'>"+bookObj.Bremain+"</div>"+
              "<div class='position'>"+bookObj.Bplace+"</div>"+
              "<div class='operation'>"+
              "<span class='add'><img src='../img/icon/modify.png'>增加</span>\n"+
              "<span class='down'><img src='../img/icon/delete.png'>减少</span>"+
              "</div></li>");
              $(".formContainer .form").append($li);
    
            }
          }
        },
        error: function (err) {
          console.log("fail")
          console.log(err);
        },
        
      });
    }
  }
  $(".userInfo .userName span").text('[ '+getCookie("adminName")+" ]");
  // 查询按钮
  $(".searchBox .submit")[0].onclick = function(){
    var searchType = $(".inputContent .way").val();
    var searchBookInfo = $(".inputContent .searchContent").val();
    bookType = searchType;
    bookInfo = searchBookInfo;
    console.log(searchType); //分别为bookName, bookAutor, BookISBN
    console.log(searchBookInfo); //用户输入的内容
    if(searchBookInfo == ''){
      alert('请先输入查询信息！')
    }else{
      $.ajax({
        type: "POST",
        url: "../php/book/searchbook.php", //PHP文件地址
        data: {searchBooInfo: searchBookInfo, searchBookType: searchType},
        // data: { AlbumName: "shanghai", Entered: "5/9/2013" },
        dataType: "json",
        success: function (response) {
          console.log(response);
          if(response == 'null'){
            alert("对不起，暂无该书籍的查询结果！")
          }else{
            $(".formContainer .form").empty();
            var $li = $("<li class='des'>"+
            "<div class='bookName'>书名</div>"+
            "<div class='bookAutor'>作者</div>"+
            "<div class='isbn'>ISBN</div>"+
            "<div class='type'>类别</div>"+
            "<div class='num'>总库存</div>"+
            "<div class='reNum'>剩余库存</div>"+
            "<div class='position'>位置</div>"+
            "<div class='operation'>库存管理</div>"+
            "</li>");
            $(".formContainer .form").append($li);
    
            for(let i = 0; i < response.length ;i++){
              var bookObj = response[i];
              var $li = $("<li class='bookResult'>"+
              "<div class='bookName'>"+bookObj.Bname+"</div>"+
              "<div class='bookAutor'>"+bookObj.Bauthor+"</div>"+
              "<div class='isbn'>"+bookObj.ISBN+"</div>"+
              "<div class='type'>"+bookObj.Bclass+"</div>"+
              "<div class='num'>"+bookObj.Bnum+"</div>"+
              "<div class='reNum'>"+bookObj.Bremain+"</div>"+
              "<div class='position'>"+bookObj.Bplace+"</div>"+
              "<div class='operation'>"+
              "<span class='add'><img src='../img/icon/modify.png'>增加</span>\n"+
              "<span class='down'><img src='../img/icon/delete.png'>减少</span>"+
              "</div></li>");
              $(".formContainer .form").append($li);
    
            }
          }
        },
        error: function (err) {
          console.log("fail")
          console.log(err);
        },
        
      });
    }
    
  }
  //增加按钮
  $(document).on('click', '.operation .add', function(e) {
 
    var bookManageName = $(this).parent(".operation").siblings(".isbn").text();
    console.log(bookManageName);
    // 首先查询这本书的数据
    $.ajax({
      type: "POST",
      url: "../php/book/searchbook.php", //PHP文件地址
      data: {searchBooInfo: bookManageName, searchBookType: 'ISBN'},
      // data: { AlbumName: "shanghai", Entered: "5/9/2013" },
      dataType: "json",
      success: function (response) {
        // 成功返回数据
        console.log(response);
        var oldBookNum = response[0].Bnum;
        var oldBookReNum = response[0].Bremain;
        var bookISBN = response[0].ISBN;
        var addBookNum = prompt("请输入增加库存的数量：");
        console.log(addBookNum);
        if(addBookNum == null){
          alert("已取消更改！");
        }else if(addBookNum <= 0 || addBookNum%1 != 0){
          alert('请输入一个大于0的整数!');
        }else if(addBookNum > 0 && confirm('是否确认增加？')){

          // 修改书的数量：
          $.ajax({
            type: "POST",
            url: "../php/book/changebooknum.php", //PHP文件地址
            data: {act: 1, ISBN: bookISBN, num: addBookNum},
            // data: { AlbumName: "shanghai", Entered: "5/9/2013" },
            dataType: "json",
            success: function (response) {
              // 成功返回数据
              console.log(response);
              if(response.success == 'update'){
                alert("添加成功！");
                getBookInfo();
              }
              
            },
            error: function (err) {
              console.log("fail")
              console.log(err);
            },
            
          });
        }
      },
      error: function (err) {
        console.log("fail")
        console.log(err);
      },
      
    });

  });
  // 减少按钮
  $(document).on('click', '.operation .down', function(e) {
 
    var bookManageName = $(this).parent(".operation").siblings(".isbn").text();
    console.log(bookManageName);
    // 首先查询这本书的数据
    $.ajax({
      type: "POST",
      url: "../php/book/searchbook.php", //PHP文件地址
      data: {searchBooInfo: bookManageName, searchBookType: 'ISBN'},
      // data: { AlbumName: "shanghai", Entered: "5/9/2013" },
      dataType: "json",
      success: function (response) {
        // 成功返回数据
        console.log(response);
        var oldBookNum = response[0].Bnum;
        var oldBookReNum = response[0].Bremain;
        var bookISBN = response[0].ISBN;
        var reduceNum = prompt("请输入减少库存的数量：");
        console.log(reduceNum);
        console.log(oldBookReNum);
        if(reduceNum == null){
          alert("已取消更改！");
        }
        else if(reduceNum <= 0 || reduceNum%1 != 0){
          alert('请输入一个大于0的整数!');
        }
        else if(parseInt(reduceNum) <= parseInt(oldBookReNum) && confirm('是否确认减少！') == true){
          
          // 修改书的数量：
          $.ajax({
            type: "POST",
            url: "../php/book/changebooknum.php", //PHP文件地址
            data: {act: 0, ISBN: bookISBN, num: reduceNum},
            // data: { AlbumName: "shanghai", Entered: "5/9/2013" },
            dataType: "json",
            success: function (response) {
              // 成功返回数据
              console.log(response);
              if(response.success == 'update'){
                alert("减少成功！");
              }
              getBookInfo();
              
            },
            error: function (err) {
              console.log("fail")
              console.log(err);
            },
            
          });

        }
        else{
          alert("更改失败！(或因减少数量大于图书现存数量)");
        }
      },
      error: function (err) {
        console.log("fail")
        console.log(err);
      },
      
    });
  });
})